package com.github.m6d21.sso.business.controller.test;

import static com.github.m6d21.sso.support.validator.password.PasswordConstant.DIFFICULTY_TIP;

import com.github.m6d21.sso.model.user.entity.User;
import com.github.m6d21.sso.model.user.qo.UserQO;
import com.github.m6d21.sso.service.user.IUserService;
import com.github.m6d21.sso.support.model.Response;
import com.github.m6d21.sso.support.util.ResponseUtil;
import com.github.m6d21.sso.support.validator.password.Password;
import com.github.m6d21.sso.support.validator.password.PasswordConstant.Type;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Date;
import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.Email;
import javax.validation.constraints.Max;
import javax.validation.constraints.NotNull;
import org.springframework.context.annotation.Scope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 *<p>
 *
 *</p>
 *
 * @author liuyang
 * @date 2018-12-07 Fri 13:27:18
 * @since 1.0.0
 */
@Api(value = "test-validator", description = "hibernate-validator测试")
@Validated
@RestController
@RequestMapping("/test/validator")
@Scope("prototype")
public class ValidatorController {

    @Resource
    private IUserService userService;

    @ApiOperation(value = "@NotNull测试", httpMethod = "GET")
    @GetMapping("/notnull")
    @ResponseBody
    public Response notNull
        (@NotNull(message = "用户名不能为空") @RequestParam("username") String username,
            @NotNull(message = "密码不能是空") @RequestParam("password") String password) {
        return ResponseUtil.success();
    }

    @ApiOperation(value = "@Max测试", httpMethod = "GET")
    @GetMapping("/max")
    @ResponseBody
    public Response max
        (@Max(value = 150, message = "年龄不能大于150") @RequestParam("age") Integer age) {
        return ResponseUtil.success();
    }

    @ApiOperation(value = "@Email测试", httpMethod = "GET")
    @GetMapping("/email")
    @ResponseBody
    public Response email
        (@Email(message = "邮箱格式不正确", regexp = ".+souche.+") @RequestParam("email") String email) {
        return ResponseUtil.success();
    }

    @ApiOperation(value = "自定义注解@Password测试", httpMethod = "GET")
    @GetMapping("/password")
    @ResponseBody
    public Response password
        (@Password(type = Type.DIFFICULTY, message = DIFFICULTY_TIP) @RequestParam("password") String password) {
        return ResponseUtil.success();
    }

    @ApiOperation(value = "实体类注解校验", httpMethod = "POST")
    @PostMapping("/entity")
    @ResponseBody
    public Response entity(@Valid @RequestBody UserQO userQO) {
        return ResponseUtil.success(userQO);
    }

    @ApiOperation(value = "时间格式", httpMethod = "POST")
    @PostMapping("/date/format")
    @ResponseBody
    public Response entity() {
        User user = new User();
        user.setUpdateTime(new Date());
        user.setCreateTime(new Date());
        return ResponseUtil.success(userService.getCurrentUser());
    }
}
